package com.finalab.yueqiu.controller.user;

import com.finalab.yueqiu.common.BeanValidator;
import com.finalab.yueqiu.constant.EntityType;
import com.finalab.yueqiu.constant.RedisKeyType;
import com.finalab.yueqiu.constant.ResponseCode;
import com.finalab.yueqiu.entity.user.User;
import com.finalab.yueqiu.service.auth.AuthServiceImpl;
import com.finalab.yueqiu.service.token.TokenServiceImpl;
import com.finalab.yueqiu.service.user.UserServiceImpl;
import com.finalab.yueqiu.util.RedisKeyUtil;
import com.finalab.yueqiu.util.ResponseUtil;
import com.qcloud.cos.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * controller接口均没有异常处理，异常处理都在common下的ExceptionResolver统一处理了。
 * @author 莉安
 */
@Controller
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;
    @Autowired
    TokenServiceImpl tokenServiceImpl;
    @Autowired
    AuthServiceImpl authServiceImpl;
    private static final String CODE = "code";


    @RequestMapping(value = "/user/logout",method = RequestMethod.POST)
    public void logout( PrintWriter out, HttpSession session){
        Integer id = (Integer) session.getAttribute("id");
        if(id == null){
            ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED.getValue());
            return;
        }
            boolean succeed = tokenServiceImpl.deleteToken(id);
        if(!succeed){
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR.getValue());
            return;
        }
            session.removeAttribute("id");
        ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED.getValue());
    }

    /**
     *
     * @param userGet
     * @param out
     * @param session
     */
    @RequestMapping(value = "/user/register",method = RequestMethod.POST)
    public void register(User userGet, PrintWriter out,
                         HttpSession session, HttpServletResponse response){

        Map<String, Object> map = new HashMap<>(1);
            if(!BeanValidator.validate(userGet)){
                ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL.getValue());
                return;
            }
            Map<String, Integer> param = userServiceImpl.register(userGet);
            if (param.get(CODE) != ResponseCode.REQUEST_SUCCESSED.getValue()) {
                ResponseUtil.responseCode(out, param.get(CODE));
                return;

            }
            Integer id = param.get("id");
        String token = tokenServiceImpl.getToken(RedisKeyUtil.getKey(id, EntityType.USER.getValue(),RedisKeyType.TOKEN));
        if(token == null){
            token = tokenServiceImpl.addToken(id);
        }
        Cookie cookie = new Cookie("token", token);
        response.addCookie(cookie);
        map.put("id", id);
        session.setAttribute("id", id);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED.getValue(), map);

        }



    @RequestMapping(value = "/user/login",method = RequestMethod.POST)
    public void login(User userGet, PrintWriter out, HttpServletResponse response,
                      HttpSession session){

        Map<String, Object> map;
        if(!BeanValidator.validate(userGet)){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL.getValue());
            return;
        }
        Map<String, Integer> param = userServiceImpl.login(userGet);
        if(param.get(CODE) != ResponseCode.REQUEST_SUCCESSED.getValue()){
            ResponseUtil.responseCode(out, param.get(CODE));
            return;
        }
        Integer id = param.get("id");
        String token = tokenServiceImpl.getToken(RedisKeyUtil.getKey(id, EntityType.USER.getValue(),RedisKeyType.TOKEN));
        if(token == null){
            token = tokenServiceImpl.addToken(id);
        }
        Cookie cookie = new Cookie("token", token);
        response.addCookie(cookie);
        userGet.setId(id);
        map = new HashMap<>(1);
        map.put("id",id);
        session.setAttribute("id", id);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED.getValue(), map);
    }


}
